package server;

import java.io.IOException;
import java.io.InputStream;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class BillingServer implements IBillingServer {

	@Override
	public IBillingServerSecure login(String username, String password) {

		if (compareHash(username, password)) {
        	
        	String name = "IBillingServerSecure";
        	IBillingServerSecure bss = new BillingServerSecure();
        	IBillingServerSecure stub = null;
			try {
				stub = (IBillingServerSecure) UnicastRemoteObject.exportObject(bss, 0);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			
			/*Registry registry;
			try {
				registry = LocateRegistry.getRegistry();
				registry.rebind(name, stub);
			} catch (RemoteException e) {
				e.printStackTrace();
			}*/
			
        	return stub;
		}
		else {
			return null;
		}

	}
	

	private boolean compareHash(String username, String password) {
		
		MessageDigest md5 = null;
		try {
			md5 = MessageDigest.getInstance("MD5");
			md5.reset();
			md5.update(password.getBytes());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
        byte[] result = md5.digest();

       
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < result.length; i++) {
            String h = Integer.toHexString(0xFF & result[i]);
            while (h.length() < 2) {
                h = "0" + h;
            }
            hexString.append(h);
        }
        
        InputStream is = ClassLoader.getSystemResourceAsStream("user.properties");
        String pass = null;
        
        if (is != null) { 
        	java.util.Properties props = new java.util.Properties(); 
        	try { 
        		props.load(is); 
        		pass = props.getProperty(username); 
        
        
        	} catch (IOException e) {
				e.printStackTrace();
			} finally { 
        		try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				} 
        	} 
        } else { 
        	System.err.println("Properties file not found!"); 
        }
        
        if (pass != null && hexString.toString().equals(pass)) {
        	return true;
        }
        else {
        	System.err.println("User or password are wrong!"); 
        	return false;
        }
	}
	
	public static void main(String[] args) {
		/*if (System.getSecurityManager() == null) {
            System.setSecurityManager(new SecurityManager());
        }*/
		
		String name = "IBillingServer";
    	
    	IBillingServer bs = new BillingServer();
    	IBillingServer stub = null;
		try {
			stub = (IBillingServer) UnicastRemoteObject.exportObject(bs, 0);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
		Registry registry;
		try {
			registry = LocateRegistry.getRegistry();
			registry = LocateRegistry.createRegistry(1099);
			
			try {
				registry.bind(name, stub);
			} catch (AlreadyBoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			registry.rebind(name, stub);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
}
